<?php
if(!defined('DCS_ROOT')) exit('Access Denied');

class contact {

	function __construct() {		
		$url = getUrl();
		$action = $url['params']['1'];			
		if($action == 'add'){
			$this->addAction();
		}elseif($action == 'cancel'){
			$this->cancelAction();
		}else{
			pageError();
		}
		exit();
	}

	function defaultAction(){
		exit('sdsd');
	}

	function addAction (){
		$url = getUrl();
		$DB = getDB();
		require_once(APP_PATH."model/people.php");
		$data = new peopleModel($DB);
		$uid = $url['params']['2'];
		!is_numeric($uid)  && pageError();
		$user = getMember();
		if(empty($user)){
			message('','/login');
		}
		$myuid = $user['uid'];
		if($myuid == $uid){
			message('您不能关注您自已。');
		}
		$isadd = $data->isContact($myuid,$uid);
		if(!empty($isadd)){
			message('您已经关注过此用户了。');
		}

		$review_id = $data->addContact($myuid,$uid);
		if(!empty($review_id)){
			$update_friend = $data->updateFriends($myuid);
			message('成功关注此用户。');
		}else{
			message('您关注此用户失败，请重新操作。');
		}
		
	}

	function cancelAction (){
		$url = getUrl();
		$DB = getDB();
		require_once(APP_PATH."model/people.php");
		$data = new peopleModel($DB);
		$uid = $url['params']['2'];
		!is_numeric($uid)  && pageError();
		$user = getMember();
		if(empty($user)){
			message('','/login');
		}
		$myuid = $user['uid'];

		if($myuid == $uid){
			message('您不能关注您自已。');
		}

		$isadd = $data->isContact($myuid,$uid);
		if(empty($isadd)){
			message('您还未关注过此用户。');
		}

		$is_cancel = $data->cancelContact($myuid,$uid);
		if(!empty($is_cancel)){
			message('成功取消关注此用户。');
		}else{
			message('您取消关注此用户失败，请重新操作。');
		}


		
	}

}
?>
